<template>
  <div class="sort">
    <top :title="title"></top>
    <div class="sortContent">
      <div class="sortLeft">
        <ul>
          <li
            v-bind:class="activateId == item.categoryId ? 'activate' : ''"
            v-for="item in sortType"
            :key="item.categoryId"
            @click="activate(item.categoryId)"
          >
            {{ item.categoryTitle }}
          </li>
        </ul>
      </div>
      <div class="sortRight">
        <div class="sortItem" v-for="item in sortItem" :key="item.sortItemId">
          <div class="sortImg">
            <img
              :src="item.cover"
              alt=""
            />
          </div>
          <div class="sortTitle">{{ item.goodsTitle }}</div>
          <div class="sortButton">
            <div class="sortPrice">￥{{ item.price }}</div>
            <div class="sortNum">
              <div class="goShopCart" @click="item.saleCount++" v-if="item.saleCount == 0">
                加入购物车
              </div>
              <template v-else>
                <div class="reduce" @click="item.saleCount == 0 ? 0 : item.saleCount--">
                  -
                </div>
                <div class="num">{{ item.saleCount }}</div>
                <div class="add" @click="item.saleCount++">+</div>
              </template>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Top from "../components/top";
export default {
  name: "Sort",
  components: {
    Top,
  },
  data() {
    return {
      title: "分类",
      activateId: '10',
      sortType: [],
      sortItem: [],
    };
  },
  mounted() {
    this.getSorttype();
    this.getSortItem();
  },
  methods: {
    activate(id) {
      this.activateId = id;
      //console.log(id)
      this.getSortItem();
    },
    getSorttype() {
      this.$http.get("/goodsCategory/getall").then((res) => {
        //console.log(res);
        let data = res.data.data;
        this.sortType = data;
        this.activateId = data[0].categoryId;
      });
    },
    getSortItem() {
      this.$http
        .get("/goods/getList?categoryId=" + this.activateId + "&page=1&size=20")
        .then((res) => {
          //console.log(res);
          let data = res.data.data.rows;
          this.sortItem = data;
          //console.log(this.sortItem);
        });
    },
  },
};
</script>

<style scoped>
.sort {
  height: calc(100% - 55px);
}
.sortContent {
  display: flex;
  height: calc(100% - 40px);
}
.sortLeft {
  flex: 1;
  background-color: WhiteSmoke;
  overflow-y: scroll;
}
.sortRight {
  box-sizing: border-box;
  flex: 3;
  padding: 0 10px;
  overflow-y: scroll;
}
.sortLeft ul {
  list-style: none;
  margin: 0;
  padding: 0;
  text-align: center;
}
.sortLeft ul li {
  padding: 15px 0;
}
.sortLeft ul .activate {
  background-color: white;
  border-left: 2px solid #d81e06;
  color: #d81e06;
}
.sortItem {
  margin-bottom: 20px;
}
.sortImg {
  width: 100%;
  height: 160px;
  overflow: hidden;
}
.sortImg img {
  width: 100%;
  height: 160px;
  object-fit: cover;
}
.sortTitle {
  font-size: 1.25rem;
  padding: 10px 0;
  overflow: hidden;
  text-overflow: ellipsis;
}
.sortButton {
  height: 30px;
  line-height: 30px;
  display: flex;
  font-size: 1.4rem;
}
.sortPrice {
  flex: 1;
  color: red;
}
.goShopCart {
  color: #fff;
  background-color: #ff2200;
  height: 30px;
  border: none;
  border-radius: 15px;
  padding: 0 15px;
  outline: none;
  font-size: 1rem;
}
.reduce {
  display: inline-block;
  box-sizing: border-box;
  border: 1px solid #ff2200;
  background-color: #fff;
  color: #ff2200;
  height: 30px;
  width: 30px;
  border-radius: 15px;
  text-align: center;
  outline: none;
  font-size: 1.25rem;
}
.num {
  display: inline-block;
  font-size: 1.25rem;
  padding: 0 15px;
}
.add {
  display: inline-block;
  color: #fff;
  background-color: #ff2200;
  height: 30px;
  width: 30px;
  border: none;
  border-radius: 15px;
  text-align: center;
  outline: none;
  font-size: 1.25rem;
}
</style>
